package org.chs.ts.batch.exception;


import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.chs.ts.base.batch.exec.entity.BatchClearBasicTaskExec;
import org.chs.ts.base.batch.exec.entity.BatchClearBasicTaskStepExec;
import org.chs.ts.base.batch.log.entity.BatchClearBasicLog;
import org.chs.ts.base.constant.CommonConstant;
import org.chs.ts.batch.constant.TaskStatusCodeEnum;
import org.chs.ts.batch.constant.TaskStepStatusCodeEnum;
import org.chs.ts.batch.factory.SernoFactory;
import org.chs.ts.batch.service.IBatchClearBasicTaskStepExecService;
import org.chs.ts.batch.threadservice.BatchServiceBase;
import org.chs.ts.batch.util.ConvertToUtil;
import org.chs.ts.base.common.util.SpringContextUtils;


import java.util.HashMap;

/**
 * @Compay 盛唐天行
 * @Author: wuwei
 * @Censor By wuwei
 * @Sence 2023/1/5 15:36
 * @Modify 2023/1/5 15:36
 * @Description 跑批 子任务 执行返回码
 */
@Getter
@Slf4j
public class TaskStepExecException extends TaskExecException {
    //private static Logger log = LoggerFactory.getLogger(TaskStepExecException.class);

    private static final String SERNO_TYPE = "taskStep";


    private final TaskStepStatusCodeEnum taskStepStatusCodeEnum;

    private IBatchClearBasicTaskStepExecService clearBasicTaskStepExecService;


    public TaskStepExecException(String str, TaskStepStatusCodeEnum taskStepStatusCodeEnum, BatchServiceBase<BatchClearBasicTaskExec, BatchClearBasicTaskStepExec> batchServiceBase) throws TaskStepExecException {

        //super(str.substring(0,1900), TaskStatusCodeEnum.TASK_RUNNING_RUNNINGERROR, batchServiceBase);
        super(str, TaskStatusCodeEnum.TASK_RUNNING_RUNNINGERROR, batchServiceBase);

        //createClearBasicLog(batchServiceBase);
        this.taskStepStatusCodeEnum = taskStepStatusCodeEnum;

        // 初始化 spring bean
        //log.info(this.getMessage());
    }

    protected void createClearBasicLog(BatchServiceBase<BatchClearBasicTaskExec, BatchClearBasicTaskStepExec> batchServiceBase) {
        super.createClearBasicLog(batchServiceBase);

        if (batchServiceBase == null || batchServiceBase.getTaskStepListObject() == null ) {
            return;
        }

        BatchClearBasicTaskStepExec taskStepExec = batchServiceBase.getTaskStepObject();
        String ip = batchServiceBase.getIp();
        String port = batchServiceBase.getPort();
        String path = batchServiceBase.getPath();
        long startTime = batchServiceBase.getTaskStepStartTime();
        String endTime = ConvertToUtil.getRunningTime();
        String execStatus = "4";

        clearBasicTaskStepExecService.updateTaskStepByObj(taskStepExec.getTaskExecid(), ip + ":" + port, path,
                ConvertToUtil.timeMillisFormatDateTime(startTime),
                ConvertToUtil.timeMillisFormatDateTime(Long.valueOf(endTime)), CommonConstant.RTN_CODE_FAILED, execStatus);

        String rtnDesc = super.getLogMessage(getMessage());
        clearBasicLogService.save(
                ConvertToUtil.convertToV(new BatchClearBasicLog(), taskStepExec, new HashMap<String, Object>() {
                    {
                        put("logSerno", SernoFactory.createSerno(SERNO_TYPE));
                        put("stepNo", taskStepExec.getStepNo());
                        put("execStatus", execStatus);
                        put("shouldExecDate", batchServiceBase.getTaskObject().getExecDate());
                        put("targetCode", "");
                        put("startTime", ConvertToUtil.timeMillisFormatDateTime(startTime));
                        put("endTime", ConvertToUtil.timeMillisFormatDateTime(Long.valueOf(endTime)));
                        put("threadUuid", "");
                        put("serverName", path);
                        put("serverIp", ip + ":" + port);
                        put("rtnCode", CommonConstant.RTN_CODE_FAILED);
                        put("rtnDesc", rtnDesc);
                    }
                })


        );
    }


    @Override
    protected void initSpring() {

        super.initSpring();
        this.clearBasicTaskStepExecService = (IBatchClearBasicTaskStepExecService) SpringContextUtils.getBean("batchClearBasicTaskStepExecServiceImpl");
    }


}
